import 'package:clean_app/asset/color.dart';
import 'package:clean_app/asset/textstyle.dart';
import 'package:clean_app/components/miw.dart';
import 'package:flutter/material.dart';

class LoadingDialog {
  static OverlayEntry? _overlayEntry;
  static bool _isVisible = false;

  static void show(BuildContext context, {String? text}) {
    // 如果已经显示，则不重复显示
    if (_isVisible) return;

    OverlayState? overlayState = Overlay.of(context);

    _overlayEntry = OverlayEntry(
      builder: (context) => Positioned(
        top: MediaQuery.of(context).size.height * 0.5 - 50,
        left: MediaQuery.of(context).size.width * 0.5 - 50,
        child: IgnorePointer(
          child: Material(
            color: Colors.transparent,
            child: Container(
              width: 100,
              height: 100,
              decoration: BoxDecoration(
                color: Colors.black.withOpacity(0.7),
                borderRadius: BorderRadius.circular(10),
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(
                    width: 30,
                    height: 30,
                    child: CircularProgressIndicator(
                      valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                      strokeWidth: 3,
                    ),
                  ),
                  if (text != null) ...[
                    SizedBox(height: 10),
                    Text(
                      text,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ],
              ),
            ),
          ),
        ),
      ),
    );

    overlayState.insert(_overlayEntry!);
    _isVisible = true;
  }

  static void dismiss(BuildContext context) {
    if (!_isVisible || _overlayEntry == null) return;

    try {
      _overlayEntry!.remove();
      _overlayEntry = null;
      _isVisible = false;
    } catch (e) {
      // 忽略错误，可能已经移除
    }
  }
}

// 自定义 Toast 组件
class Toast {
  static void showInCenter({
    required String text,
    required BuildContext context,
    Duration duration = const Duration(seconds: 2),
  }) {
    OverlayState? overlayState = Overlay.of(context);
    late OverlayEntry overlayEntry;

    overlayEntry = OverlayEntry(
      builder: (context) => Center(
        // top: MediaQuery.of(context).size.height * 0.5,
        // left: MediaQuery.of(context).size.width * 0.5,
        child: IgnorePointer(
          child: Material(
            color: Colors.transparent,
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
              decoration: BoxDecoration(
                color: Colors.black.withOpacity(0.7),
                borderRadius: BorderRadius.circular(20),
              ),
              child: Text(
                text,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 16,
                ),
              ),
            ),
          ),
        ),
      ),
    );

    overlayState.insert(overlayEntry);

    Future.delayed(duration, () {
      try {
        overlayEntry.remove();
      } catch (e) {
        // 忽略错误，可能已经移除
      }
    });
  }
}

void showInputModal({
  required BuildContext context,
  String? title,
  String? hint,
  String? confirmTxt,
  String? cancelTxt,
  Function? confirm,
  Function? cancel,
}) {
  TextEditingController controller = TextEditingController();

  confirmSubmit(String res) {
    if (confirm != null) {
      confirm(res);
    }
    Navigator.pop(context);
  }

  AlertDialog alert = AlertDialog(
    backgroundColor: os_white,
    surfaceTintColor: os_white,
    shape: const RoundedRectangleBorder(
      borderRadius: BorderRadius.all(
        Radius.circular(15),
      ),
    ),
    title: Padding(
      padding: const EdgeInsets.all(5.0),
      child: Center(
        child: Text(
          title ?? "标题",
          style: XSTextStyle(
            context: context,
            listenProvider: false,
            fontWeight: FontWeight.bold,
            fontSize: 18,
            color: os_black,
          ),
        ),
      ),
    ),
    content: ConstrainedBox(
      constraints: BoxConstraints(
        maxWidth: 380,
        minWidth: 350,
      ),
      child: TextField(
        keyboardAppearance: Brightness.light,
        autofocus: true,
        controller: controller,
        onSubmitted: (res) {
          confirmSubmit(res);
        },
        decoration: InputDecoration(
          fillColor: os_grey,
          filled: true,
          hintText: hint ?? "请输入",
          hintStyle: XSTextStyle(
            context: context,
            listenProvider: false,
            color: os_deep_grey,
          ),
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(10),
            borderSide: BorderSide.none,
          ),
        ),
      ),
    ),
    actions: [
      Container(
        // padding: const EdgeInsets.only(bottom: 8.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            cancelTxt == ""
                ? Container()
                : myInkWell(
                    color: Colors.transparent,
                    widget: Container(
                      // width: 130,
                      padding: EdgeInsets.symmetric(horizontal: 20),
                      height: 50,
                      child: Center(
                        child: Text(
                          cancelTxt ?? "取消",
                          style: XSTextStyle(
                            context: context,
                            listenProvider: false,
                            fontSize: 15,
                            fontWeight: FontWeight.w500,
                            color: os_black,
                          ),
                        ),
                      ),
                    ),
                    radius: 10,
                    tap: () {
                      Navigator.pop(context);
                      if (cancel != null) cancel();
                    },
                  ),
            cancelTxt == "" ? Container() : Container(width: 10),
            myInkWell(
              color: os_trans,
              widget: Container(
                height: 50,
                padding: EdgeInsets.symmetric(horizontal: 20),
                child: Center(
                  child: Text(
                    confirmTxt ?? "确认",
                    style: XSTextStyle(
                      context: context,
                      listenProvider: false,
                      color: os_color,
                      fontSize: 15,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              radius: 10,
              tap: () {
                confirmSubmit(controller.text);
              },
            ),
          ],
        ),
      ),
    ],
  );
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}

void showModal({
  required BuildContext context,
  String? title,
  String? cont,
  String? confirmTxt,
  String? cancelTxt,
  Function? confirm,
  Function? cancel,
}) {
  AlertDialog alert = AlertDialog(
    backgroundColor: os_white,
    surfaceTintColor: os_white,
    shape: const RoundedRectangleBorder(
      borderRadius: BorderRadius.all(
        Radius.circular(15),
      ),
    ),
    title: Padding(
      padding: const EdgeInsets.all(5.0),
      child: Center(
        child: Text(
          title ?? "标题",
          style: XSTextStyle(
            context: context,
            listenProvider: false,
            fontWeight: FontWeight.bold,
            fontSize: 18,
            color: os_black,
          ),
        ),
      ),
    ),
    content: ConstrainedBox(
      constraints: BoxConstraints(
        maxWidth: 300,
      ),
      child: Text(
        cont ?? "内容",
        textAlign: TextAlign.center,
        style: XSTextStyle(
          context: context,
          listenProvider: false,
          fontSize: 15,
          color: os_black,
        ),
      ),
    ),
    actions: [
      Container(
        // padding: const EdgeInsets.only(bottom: 8.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            cancelTxt == ""
                ? Container()
                : myInkWell(
                    color: Colors.transparent,
                    widget: Container(
                      // width: 130,
                      padding: EdgeInsets.symmetric(horizontal: 20),
                      height: 50,
                      child: Center(
                        child: Text(
                          cancelTxt ?? "取消",
                          style: XSTextStyle(
                            context: context,
                            listenProvider: false,
                            fontSize: 15,
                            fontWeight: FontWeight.w500,
                            color: os_black,
                          ),
                        ),
                      ),
                    ),
                    radius: 10,
                    tap: () {
                      Navigator.pop(context);
                      if (cancel != null) cancel();
                    },
                  ),
            cancelTxt == "" ? Container() : Container(width: 10),
            myInkWell(
              color: os_trans,
              widget: Container(
                height: 50,
                padding: EdgeInsets.symmetric(horizontal: 20),
                child: Center(
                  child: Text(
                    confirmTxt ?? "确认",
                    style: XSTextStyle(
                      context: context,
                      listenProvider: false,
                      color: os_color,
                      fontSize: 15,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              radius: 10,
              tap: () {
                Navigator.pop(context);
                if (confirm != null) confirm();
              },
            ),
          ],
        ),
      ),
    ],
  );
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}
